﻿/**
* QSmart Andon
*
* 2012-10-11
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/

using System;
using System.Collections;
using System.Web;
using System.Web.SessionState;
using FX.CC.Andon.Business.BLL.SystemManage;
using FX.CC.Andon.Business.SystemManage;
using FX.CC.Andon.Entity;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Entity.SystemManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.WebSite.Service
{
    /// <summary>
    /// SystemManageHandler 的摘要说明
    /// </summary>
    public class SystemManageHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                var action = DtRequest.GetQueryString("action");
                if (string.IsNullOrEmpty(action)) return;
                var method = this.GetType().GetMethod(action);
                if (method == null) return;
                object[] parameters = { context };
                method.Invoke(this, parameters);
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        #region 权限中心
        public void GetMenuIdForRoleRight(HttpContext context)
        {
            try
            {
                var roleId = context.Request["RoleID"];
                if (String.IsNullOrEmpty(roleId)) return;
                var json = new RoleRightBusiness().GetMenuIdForRoleRight(roleId);
                context.Response.Write(json);
                context.Response.End();
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        public void SetRoleRight(HttpContext context)
        {
            var data = context.Request["data"];
            var json = new RoleRightBusiness().SetRoleRight(data);
            context.Response.Write(json);
            context.Response.End();
        }
        #endregion

        #region 部门
        public void GetDeptInfo(HttpContext context)
        {
            try
            {
                var dptId = context.Request["DptID"];
                if (String.IsNullOrEmpty(dptId)) return;
                var json = new DeptBusiness().GetDeptInfo(dptId);
                context.Response.Write(json);
                context.Response.End();
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        public void RemoveDept(HttpContext context)
        {
            try
            {
                var dptId = context.Request["DptID"];
                if (String.IsNullOrEmpty(dptId)) return;
                new DeptBusiness().Remove(dptId);
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        public void GetDeptList(HttpContext context)
        {
            try
            {

                var bll = new DeptBusiness();
                var qcmodel = new DepartmentQcEntity
                {
                    DptName = context.Request["DptName"],
                    PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                    PageSize = Convert.ToInt32(context.Request["pageSize"]),
                    SortField = context.Request["sortField"],
                    SortOrder = context.Request["sortOrder"]
                };
                var json = bll.GetDeptList(qcmodel);
                context.Response.Write(json);
                context.Response.End();

            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }
        public void AddDept(HttpContext context)
        {
            var data = context.Request["data"];
            new DeptBusiness().AddDept(data);
        }
        public void ModifyDept(HttpContext context)
        {
            var data = context.Request["data"];
            new DeptBusiness().ModifyDept(data);
        }
        #endregion

        #region 角色
        public void GetRoleList(HttpContext context)
        {
            try
            {

                var bll = new RoleBusiness();
                var qcmodel = new RoleQcEntity
                {
                    RoleName = context.Request["RoleName"],
                    PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                    PageSize = Convert.ToInt32(context.Request["pageSize"]),
                    SortField = context.Request["sortField"],
                    SortOrder = context.Request["sortOrder"]
                };
                var json = bll.GetRoleList(qcmodel);
                context.Response.Write(json);
                context.Response.End();

            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }
        public void RemoveRole(HttpContext context)
        {
            try
            {
                var roleId = context.Request["RoleID"];
                if (String.IsNullOrEmpty(roleId)) return;
                new RoleBusiness().Remove(roleId);
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }
        public void GetRoleInfo(HttpContext context)
        {
            try
            {
                var roleId = context.Request["RoleID"];
                if (String.IsNullOrEmpty(roleId)) return;
                var json = new RoleBusiness().GetRoleInfo(roleId);
                context.Response.Write(json);
                context.Response.End();
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }
        public void ModifyRole(HttpContext context)
        {
            var data = context.Request["data"];
            new RoleBusiness().ModifyRole(data);
        }
        public void AddRole(HttpContext context)
        {
            var data = context.Request["data"];
            new RoleBusiness().AddRole(data);
        }
        public void GetRoleCount(HttpContext context)
        {
            try
            {
                var roleId = context.Request["RoleID"];
                if (String.IsNullOrEmpty(roleId)) return;
                var json = new RoleBusiness().GetRoleCount(roleId);
                context.Response.Write(json);
                context.Response.End();
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        #endregion

        #region 用户
        public void GetLoginCode(HttpContext context)
        {
            var loginCode = SysContext.CurrentLoginCode;
            if (string.IsNullOrEmpty(loginCode)) return;
            context.Response.Write(loginCode);
            context.Response.End();
        }

        public void GetUserInfo(HttpContext context)
        {
            try
            {
                var userId = context.Request["UserID"];
                if (String.IsNullOrEmpty(userId)) return;
                var json = new UserBusiness().GetUserInfo(userId);
                context.Response.Write(json);
                context.Response.End();
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        public void GetIsNullQCode(HttpContext context)
        {
            try
            {
                new UserBusiness().GetIsNullQCode();
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        public void UnlockUser(HttpContext context)
        {
            try
            {
                var userId = context.Request["UserID"];
                if (String.IsNullOrEmpty(userId)) return;
                new UserBusiness().UnlockUser(userId);
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        public void LockUser(HttpContext context)
        {
            try
            {
                var userId = context.Request["UserID"];
                if (String.IsNullOrEmpty(userId)) return;
                new UserBusiness().LockUser(userId);
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        public void GetUserList(HttpContext context)
        {
            try
            {

                var bll = new UserBusiness();
                var qcmodel = new UserQcEntity
                {
                    LoginCode = context.Request["aLoginCode"],
                    FullName = context.Request["FullName"],
                    DptID = context.Request["DptID"],
                    RoleID = context.Request["RoleID"],
                    RecStatus = context.Request["RecStatus"],
                    MobileNo = context.Request["MobileNo"],
                    PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                    PageSize = Convert.ToInt32(context.Request["pageSize"]),
                    SortField = context.Request["sortField"],
                    SortOrder = context.Request["sortOrder"]
                };
                var json = bll.GetUserList(qcmodel);
                context.Response.Write(json);
                context.Response.End();

            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        public void GetIsOperatorUserList(HttpContext context)
        {
            try
            {

                var bll = new UserBusiness();
                var qcmodel = new UserQcEntity
                {
                    LoginCode = context.Request["LoginCode"],
                    FullName = context.Request["FullName"],
                    DptID = context.Request["DptID"],
                    RecStatus = context.Request["RecStatus"],
                    MobileNo = context.Request["MobileNo"],
                    PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                    PageSize = Convert.ToInt32(context.Request["pageSize"]),
                    SortField = context.Request["sortField"],
                    SortOrder = context.Request["sortOrder"]
                };
                var json = bll.GetIsOperatorUserList(qcmodel);
                context.Response.Write(json);
                context.Response.End();

            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }
        }

        public void AddUser(HttpContext context)
        {
            var data = context.Request["data"];
            new UserBusiness().AddUser(data);
        }
        public void ModifyUser(HttpContext context)
        {
            var data = context.Request["data"];
            new UserBusiness().ModifyUser(data);
        }
        public void ModifyPwd(HttpContext context)
        {
            var data = context.Request["data"];
            var json = new UserBusiness().ModifyPwd(data);
            context.Response.Write(json);
            context.Response.End();
        }
        public void ExportUser(HttpContext context)
        {
            var bll = new UserBusiness();
            var qcmodel = new UserQcEntity
            {
                LoginCode = context.Request["aLoginCode"],
                FullName = context.Request["FullName"],
                DptID = context.Request["DptID"],
                RoleID = context.Request["RoleID"],
                RecStatus = context.Request["RecStatus"],
                MobileNo = context.Request["MobileNo"]

            };
            var dt = bll.ExportUser(qcmodel);
            if (dt.Rows.Count <= 0) return;
            const bool specificTime = true;
            dt.Columns[0].ColumnName = "状态";
            dt.Columns[1].ColumnName = "登录账号";
            dt.Columns[2].ColumnName = "职员姓名";
            dt.Columns[3].ColumnName = "手机号码";
            dt.Columns[4].ColumnName = "角色";
            dt.Columns[5].ColumnName = "部门";
            dt.Columns[6].ColumnName = "描述";
            if (dt.Rows.Count > 0)
            {
                ExcelHelper.ExportByWeb(dt, "暗灯系统-用户管理", "暗灯系统-用户管理.xls", specificTime);
            }

        }

        #endregion

        #region 菜单
        public void GetSysMenuList(HttpContext context)
        {
            var json = new SysMenuBusiness().GetSysMenuList();
            context.Response.Write(json);
            context.Response.End();
        }

        public void GetSysMenuParentNode(HttpContext context)
        {
            try
            {
                var json = new SysMenuBusiness().GetSysMenuParentNode();
                context.Response.Write(json);
                context.Response.End();
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }

        }

        public void GetSysMenuChildNode(HttpContext context)
        {
            try
            {
                var menuid = context.Request["MenuID"];
                if (!string.IsNullOrEmpty(menuid))
                {
                    var bll = new SysMenuBusiness();
                    var qcmodel = new SysMenuQcEntity
                    {
                        MenuID = menuid,
                        PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                        PageSize = Convert.ToInt32(context.Request["pageSize"]),
                        SortField = context.Request["sortField"],
                        SortOrder = context.Request["sortOrder"]
                    };
                    var json = bll.GetSysMenuChildNode(qcmodel);
                    context.Response.Write(json);
                    context.Response.End();
                }
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }

        }

        public void RemoveSysMenu(HttpContext context)
        {
            var menuId = context.Request["MenuID"];
            if (String.IsNullOrEmpty(menuId)) return;
            new SysMenuBusiness().Remove(menuId);

        }


        public void RemoveParentMenuNode(HttpContext context)
        {
            var menuId = context.Request["MenuID"];
            if (String.IsNullOrEmpty(menuId)) return;
            new SysMenuBusiness().RemoveParentMenuNode(menuId);
        }

        public void GetSysMenuInfo(HttpContext context)
        {
            try
            {
                var menuId = context.Request["MenuID"];
                if (String.IsNullOrEmpty(menuId)) return;
                var json = new SysMenuBusiness().GetSysMenuInfo(menuId);
                context.Response.Write(json);
                context.Response.End();
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }

        }

        public void ModifyParentMenuNode(HttpContext context)
        {
            var data = context.Request["data"];
            new SysMenuBusiness().ModifyParentMenuNode(data);
        }
        public void AddParentMenuNode(HttpContext context)
        {
            var data = context.Request["data"];
            new SysMenuBusiness().AddParentMenuNode(data);
        }

        public void AddMenu(HttpContext context)
        {
            var data = context.Request["data"];
            new SysMenuBusiness().AddSysMenu(data);
        }
        public void ModifyMenu(HttpContext context)
        {
            var data = context.Request["data"];
            new SysMenuBusiness().ModifySysMenu(data);
        }
        #endregion

        #region 公告
        public void GetNoticeList(HttpContext context)
        {
            var bll = new NoticeBLL();
            var qcmodel = new Pagination
            {
                PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                PageSize = Convert.ToInt32(context.Request["pageSize"]),
                SortField = context.Request["sortField"],
                SortOrder = context.Request["sortOrder"]
            };
            var json = bll.GetNoticeList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        public void AddNotice(HttpContext context)
        {
            var data = context.Request["data"];
            var model = new NoticeEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.NoticeId = Guid.NewGuid();
                model.NoticeTitle = row["NoticeTitle"].ToString();
                model.AddUser = SysContext.CurrentFullName;
                model.Flag = row["Flag"].ToString() == "1";
            }

            new NoticeBLL().AddNotice(model);
        }
        public void ModifyNotice(HttpContext context)
        {
            var data = context.Request["data"];
            var model = new NoticeEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.NoticeId = new Guid(row["NoticeId"].ToString());
                model.NoticeTitle = row["NoticeTitle"].ToString();
                model.AddUser = SysContext.CurrentFullName;
                model.Flag = row["Flag"].ToString() == "1";
            }
            new NoticeBLL().ModifyNotice(model);
        }
        public void GetNoticeInfo(HttpContext context)
        {
            var id = context.Request["NoticeId"];
            if (string.IsNullOrEmpty(id)) return;
            var json = new NoticeBLL().GetNoticeInfo(id);
            context.Response.Write(json);
            context.Response.End();
        }
        public void RemoveNotice(HttpContext context)
        {
            var id = context.Request["NoticeId"];
            if (string.IsNullOrEmpty(id)) return;
            var json = new NoticeBLL().RemoveNotice(id);
            context.Response.Write(json);
            context.Response.End();
        }
        #endregion

        #region 触发人员对应产线
        public void GetLineListById(HttpContext context)
        {
            var bll = new OpLinesBusiness();
            var qcmodel = new OpLinesQcEntity
            {
                UserID = context.Request["UserID"],
                PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                PageSize = Convert.ToInt32(context.Request["pageSize"]),
                SortField = context.Request["sortField"],
                SortOrder = context.Request["sortOrder"]
            };
            var json = bll.GetLineListById(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        public void GetOpLinesList(HttpContext context)
        {
            var bll = new OpLinesBusiness();
            var qcmodel = new OpLinesQcEntity
            {
                LoginCode = context.Request["aLoginCode"],
                FullName = context.Request["FullName"],
                PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                PageSize = Convert.ToInt32(context.Request["pageSize"]),
                SortField = context.Request["sortField"],
                SortOrder = context.Request["sortOrder"]
            };
            var json = bll.GetOpLinesList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        public void AddOpLines(HttpContext context)
        {
            var data = context.Request["data"];
            new OpLinesBusiness().AddOpLines(data);
        }
        public void RemoveOpLinesByUserID(HttpContext context)
        {
            var id = context.Request["UserID"];
            if (String.IsNullOrEmpty(id)) return;
            new OpLinesBusiness().RemoveOpLinesByUserID(id);
        }
        public void RemoveOpLinesByRowID(HttpContext context)
        {
            var id = context.Request["RowID"];
            if (String.IsNullOrEmpty(id)) return;
            new OpLinesBusiness().RemoveOpLinesByRowID(id);
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}